# coding:utf-8
#1-1初探异常------------------------------------------------------------

# def upper(str_data):
#     new_str = 'None'
#     try:
#         new_str = str_data.upper()
#     except Exception as e:
#         print('程序出错了:{}'.format(e))
#     return new_str
#
# result = upper(1)
# print('result:',result)
#
# def test():
#     try:
#         print('123')
#         1/0
#         print('hello')
#     except ZeroDivisionError as e:
#         print(e)
#
# test()
#
# def test1():
#     try:
#         print('hello')
#         print(name)
#     except (ZeroDivisionError,NameError) as e:
#         print(e)
#         print(type(e))
#         print(dir(e))
#
# test1()

#2常用异常类型-------------------------------------------------------------------------

# class Test(object):
#     pass
#
# t = Test()
# try:
#     t.name
# except AttributeError as e:
#     print(e)
#
#
# d = {'name':'小慕'}
# try:
#     d['age']
# except KeyError as e:
#     print('没有对应的键：',e)
#
# l =[1,2,3]
# try:
#     l[5]
# except IndexError as e:
#     print(e)
#
# name = 'dewei'
# try:
#     int(name)
# except ValueError as e:
#     print(e)
#
# def test(a):
#     return a
#
# try:
#     test()
# except TypeError as e:
#     print(e)

#3异常中的finally---------------------------------------------------------------------------------
# def test1():
#     try:
#         1/0
#     except Exception as e:
#         print(e)
#     finally:
#         return 'finally'
#
# result = test1()
# print(result)
# print('---------------------------')
# def test2():
#     try:
#         1/0
#     except Exception as e:
#         print('111')
#         return e
#     finally:
#         print('finally')
# result = test2()
# print(result)
#
# def test3():
#     try:
#         print('try test 11')
#         return 'try'
#     except Exception as e:
#         print('e')
#     finally:
#         print('finally test')
#
# print('------------------')
# result = test3()
# print(result)
#
#
# def test4():
#     try:
#         1/0
#     except Exception as e:
#         print('1')
#         return e
#     finally:
#         print('2')
#         return 'finally'
# print('---------')
# result =test4()
# print(result)
#
# def test5():
#     try:
#         print('1try')
#         return 'try'
#     except Exception as e:
#         return 'e'
#     finally:
#         print('2finally')
#         return 'finally'
# print('===========')
# result = test5()
# print(result)
#
# def test6():
#     try:
#         print('try1')
#         1/0
#         print('try2')
#     finally:
#         return 'i am finally'
# print('==============')
# result = test6()
# print(result)

#4自定义异常与抛出异常------------------------------------------------------------
#
# def test(number):
#     if number ==100:
#         raise ValueError('number不可以是100')
#     return  number
#
# # result = test(50)
# # print(result)
#
# # result = test(100)
#
# def test2(number):
#     try:
#         return test(number)
#     except ValueError as e:
#         return e
#
# # result = test2(100)
# # print(result)
#
# def test3():
#     raise '错误'
#
# def test4(name):
#     if name =='dewei':
#         raise Exception('dewei不能被填写')
#     return name
#
# test4('dewei')
#
class NumberLimitError(Exception):
    def __init__(self,message):
        self.message = message


class NameLimitError(Exception):
    def __init__(self,message):
        self.message = message


def test5(name):
    if name == 'dewei':
        raise NameLimitError('dewei不能被填写')
    return name

def test6(number):
    if number > 100:
        raise NumberLimitError('数字不可以大于100')
    return number
print('-----------')

try:
    test5('dewei')
except NameLimitError as e:
    print(e)

try:
    test6(102)
except NumberLimitError as e:
    print(e)



